package com.gress.shop.service.impl;

import com.gress.shop.bo.Member;
import com.gress.shop.constants.EmailConstants;
import com.gress.shop.service.mail.IMailService;
import com.gress.shop.service.inner.IMemberInternalService;
import com.gress.shop.service.IMemberService;
import com.gress.shop.util.BaseUtil;
import com.gress.shop.vo.SwalFactory;
import com.gress.shop.vo.SwalReturn;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Service
public class MemberService implements IMemberService {
    @Resource
    private IMemberInternalService memberInternalService;
    @Resource
    private IMailService NTESMailServiceImpl;

    @Override
    public SwalReturn registerMember(Member member, String code, HttpSession session) {
        String email = member.getEmail();
        String checkCode = (String) session.getAttribute(BaseUtil.getEmailCode(email, EmailConstants.EMAIL_CODE_REGISTER));
        // 没有拿到验证码就认为验证码已经失效,可以重新发送
        if (StringUtils.isEmpty(checkCode)) {
            //发送验证码
            SwalReturn emailReturn = NTESMailServiceImpl.sendRegisterMailCode(member.getEmail(), EmailConstants.EMAIL_CODE_REGISTER, session);
            if (!emailReturn.isSuccess()) {
                return emailReturn.setText("系统异常");
            }
            SwalReturn back = SwalFactory.getWarnSwal("验证码已经失效,已经重新发送请及时查收");
            return back;
        }
        if (StringUtils.isEmpty(checkCode) || !checkCode.equals(code)) {
            SwalReturn back = SwalFactory.getErrorSwal();
            back.setText("验证码错误,请重新输入");
            return back;
        }
        session.removeAttribute(email);
        memberInternalService.insertMember(member);
        SwalReturn back = SwalFactory.getSuccessSwal();
        back.setText("注册成功");
        return back;
    }



    @Override
    public boolean checkEmail(String email) {
        if (memberInternalService.queryByEmail(email) != null) {
            return true;
        }
        return false;
    }

    @Override
    public SwalReturn sendForgetEmail(String email, HttpSession session) {
        return NTESMailServiceImpl.sendForgetMailCode(email, EmailConstants.EMAIL_CODE_FORGET, session);
    }

    @Override
    public SwalReturn updateMemberPsd(Member member, String code, HttpSession session) {
        String backCode = NTESMailServiceImpl.checkEqualCode(code, BaseUtil.getEmailCode(member.getEmail(), EmailConstants.EMAIL_CODE_FORGET), session);
        if (EmailConstants.EMAIL_CHECK_SUCCESS.equals(backCode)) {
            boolean b = memberInternalService.updateMemberPSDByEmail(member.getEmail(), member.getPassword());
            if (b) {
                return SwalFactory.getSuccessSwal();
            }else {
                return SwalFactory.getWarnSwal("更新失败");
            }
        } else if (EmailConstants.EMAIL_OVERDUE.equals(backCode)) {
            NTESMailServiceImpl.sendForgetMailCode(member.getEmail(), EmailConstants.EMAIL_CODE_FORGET, session);
            return SwalFactory.getWarnSwal(EmailConstants.EMAIL_EXIST_RESEND);
        }else {
            return SwalFactory.getWarnSwal(EmailConstants.EMAIL_EXIST_CHECK_FAIL);
        }
    }
}
